package com.farsight.leo.fssmartlock.db;

import com.farsight.leo.fssmartlock.net.bean.AuthCodeData;
import com.farsight.leo.fssmartlock.utils.DateUtils;

import java.text.ParseException;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;

/**
 * Author: Administrator
 * Date: 2019/9/17
 * Project: SmartLockApp
 * Description:
 */
@Entity(tableName = "tbl_auth_code", indices = {@Index(value = {"_id"}, unique = true)})
public class AuthCodeEntity {
    @PrimaryKey
    @ColumnInfo(name = "_id")
    private int id;
    @ColumnInfo(name = "auth_code")
    private String authCode;
    @ColumnInfo(name = "device_id")
    private int deviceId;
    @ColumnInfo(name = "valid_date")
    private long validDate;
    @ColumnInfo(name = "user_id")
    private int userId;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAuthCode() {
        return authCode;
    }

    public void setAuthCode(String authCode) {
        this.authCode = authCode;
    }

    public int getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(int deviceId) {
        this.deviceId = deviceId;
    }

    public long getValidDate() {
        return validDate;
    }

    public void setValidDate(long validDate) {
        this.validDate = validDate;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    @Override
    public String toString() {
        return "AuthCodeEntity{" +
                "id=" + id +
                ", authCode='" + authCode + '\'' +
                ", deviceId=" + deviceId +
                ", validDate=" + validDate +
                ", userId=" + userId +
                '}';
    }

    public void importData(AuthCodeData code, int userId) {
        this.id = code.getId();
        this.authCode = code.getAuth_code();
        this.deviceId = code.getDevice_id();
        try {
            this.validDate = DateUtils.parseDefaultTimeFormatToLong(code.getValid_date());
        } catch (ParseException e) {
            e.printStackTrace();
            this.validDate = 0;
        }
        this.userId = userId;
    }
}
